package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.RuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: baiml
 * @Title: AbstractRulePublisher
 * @ProjectName: 中台技术平台
 * @Depart 中台研发部
 * @Description:
 * @date: 2021/8/16 13:40
 */
public  abstract  class AbstractRulePublisher<P extends RuleEntity> implements DynamicRulePublisher<List<P>> {

    @Autowired
    private ConfigService configService;


    protected abstract String getRulePostFix(String appName);

    @Override
    public void publish(String app, List<P> rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        //publish to nacos conf center,发布成json的格式
        configService.publishConfig(getRulePostFix(app),
                NacosConfigUtil.GROUP_ID, NacosConfigUtil.convertToRule(rules), ConfigType.JSON.getType());
    }
}
